module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-recess');
  grunt.loadNpmTasks('grunt-karma');

  // Default task.
  grunt.registerTask('default', ['build','karma:unit']);
  grunt.registerTask('build', ['concat','recess:build','copy:assets']);
  grunt.registerTask('release', ['uglify','jshint','karma:unit','concat:index', 'recess:min','copy:assets']);
  grunt.registerTask('test-watch', ['karma:watch']);

  // Print a timestamp (useful for when watching)
  grunt.registerTask('timestamp', function() {
    grunt.log.subhead(Date());
  });

  var karmaConfig = function(configFile, customOptions) {
    var options = { configFile: configFile, keepalive: true };
    //var travisOptions = process.env.TRAVIS && { browsers: ['Firefox'], reporters: 'dots' };
    //return grunt.util._.extend(options, customOptions, travisOptions);
    return grunt.util._.extend(options, customOptions);
  };

  // Project configuration.
  grunt.initConfig({
    distdir: 'dist',
    pkg: grunt.file.readJSON('package.json'),
    banner:
    '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd") %>\n' +
    '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' +
    ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author %>;\n' +
    ' * Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %>\n */\n',
    src: {
      js: ['src/**/*.js'],
      specs: ['test/**/*.spec.js'],
      scenarios: ['test/**/*.scenario.js'],
      less: ['src/less/stylesheet.less'], // recess:build doesn't accept ** in its file patterns
      lessWatch: ['src/less/**/*.less']
    },
    clean: ['<%= distdir %>/*'],
    copy: {
      assets: {
        files: [
			{ dest: '<%= distdir %>', src : '**', expand: true, cwd: 'src/assets/' }
			// 拷贝ace文件
			, { expand: true, cwd: 'vendor/font/', src: '**', dest: '<%= distdir %>/font' }
		]
      }
    },
    karma: {
      unit: { options: karmaConfig('test/config/unit.js') },
      watch: { options: karmaConfig('test/config/unit.js', { singleRun:true, autoWatch: true}) }
    },
    concat:{
      dist:{
        options: {
          banner: "<%= banner %>"
        },
        src:['<%= src.js %>'],
        dest:'<%= distdir %>/js/<%= pkg.name %>.js'
      },
      lib: {
        src:['vendor/lib/*.js'],
        dest: '<%= distdir %>/lib/jquery-extend.js'
      }
    },
    uglify: {
      dist:{
        options: {
          banner: "<%= banner %>"
        },
        src:['<%= src.js %>'],
        dest:'<%= distdir %>/js/<%= pkg.name %>.js'
      },
      jquery: {
        src:['vendor/jquery/*.js'],
        dest: '<%= distdir %>/lib/jquery.js'
      }
    },
    recess: {
      build: {
        files: {
          '<%= distdir %>/css/<%= pkg.name %>.css':['<%= src.less %>'] 
        },
        options: {
          compile: true
        }
      },
      min: {
        files: {
          '<%= distdir %>/css/<%= pkg.name %>.css': ['<%= src.less %>']
        },
        options: {
          compress: true
        }
      }
    },
    watch:{
      all: {
        files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>'],
        tasks:['default','timestamp']
      },
      build: {
        files:['<%= src.js %>', '<%= src.specs %>', '<%= src.lessWatch %>'],
        tasks:['build','timestamp']
      }
    },
    jshint:{
      files:['gruntFile.js', '<%= src.js %>', '<%= src.specs %>', '<%= src.scenarios %>'],
      options:{
        curly:true, // 循环或者条件语句必须使用花括号包围
        eqeqeq:true, // 强制使用三等号
        latedef:true, // 禁止定义之前使用变量，忽略 function 函数声明
        newcap:true, // 构造器函数首字母大写
        noarg:true, // 禁止使用 arguments.caller 和 arguments.callee ，未来会被弃用， ECMAScript 5 禁止使用 arguments.callee
        sub:true, // person['name'] vs. person.name
        boss:true, // 控制“缺少分号”的警告
        eqnull:true, // This option suppresses warnings about == null comparisons
        globals:{
			$: false,
			jQuery: false
		}
      }
    }
  });

};
